package org.jeecg.modules.demo.roomapartdistribute.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.applyinfo.entity.ZnzfApply;
import org.jeecg.modules.demo.applyinfo.service.IZnzfApplyService;
import org.jeecg.modules.demo.checkemployee.entity.ZnzfCheckEmployee;
import org.jeecg.modules.demo.checkemployee.service.IZnzfCheckEmployeeService;
import org.jeecg.modules.demo.projectroom.entity.ZnzfProject;
import org.jeecg.modules.demo.projectroom.entity.ZnzfRoom;
import org.jeecg.modules.demo.projectroom.service.IZnzfProjectService;
import org.jeecg.modules.demo.projectroom.service.IZnzfRoomService;
import org.jeecg.modules.demo.roomapartdistribute.entity.*;
import org.jeecg.modules.demo.roomapartdistribute.service.IZnzfCheckApartRoomListService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.roomapartdistribute.service.IZnzfCheckApartService;
import org.jeecg.modules.demo.roomapartdistribute.service.IZnzfRoomApartDistributeService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 核定单位房源列表
 * @Author: jeecg-boot
 * @Date:   2022-03-22
 * @Version: V1.0
 */
@Api(tags="核定单位房源列表")
@RestController
@RequestMapping("/roomapartdistribute/znzfCheckApartRoomList")
@Slf4j
public class ZnzfCheckApartRoomListController extends JeecgController<ZnzfCheckApartRoomList, IZnzfCheckApartRoomListService> {
	@Autowired
	private IZnzfCheckApartRoomListService znzfCheckApartRoomListService;

	 @Autowired
	 private ISysUserService sysUserService;

	 @Autowired
	 private IZnzfRoomApartDistributeService znzfRoomApartDistributeService;

	 @Autowired
	 private IZnzfCheckApartService znzfCheckApartService;

	 @Autowired
	 private IZnzfRoomService znzfRoomService;

	 @Autowired
	 private IZnzfApplyService znzfApplyService;

	 @Autowired
	 private IZnzfProjectService znzfProjectService;

	 @Autowired
	 private ISysDepartService sysDepartService;

	 @Autowired
	 private IZnzfCheckEmployeeService znzfCheckEmployeeService;




	 /**
	 * 分页列表查询
	 *
	 * @param znzfCheckApartRoomList
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "核定单位房源列表-分页列表查询")
	@ApiOperation(value="核定单位房源列表-分页列表查询", notes="核定单位房源列表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZnzfCheckApartRoomList>> queryPageList(ZnzfCheckApartRoomList znzfCheckApartRoomList,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZnzfCheckApartRoomList> queryWrapper = QueryGenerator.initQueryWrapper(znzfCheckApartRoomList, req.getParameterMap());
		Page<ZnzfCheckApartRoomList> page = new Page<ZnzfCheckApartRoomList>(pageNo, pageSize);
		IPage<ZnzfCheckApartRoomList> pageList = znzfCheckApartRoomListService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param znzfCheckApartRoomList
	 * @return
	 */
	@AutoLog(value = "核定单位房源列表-添加")
	@ApiOperation(value="核定单位房源列表-添加", notes="核定单位房源列表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZnzfCheckApartRoomList znzfCheckApartRoomList) {
		znzfCheckApartRoomListService.save(znzfCheckApartRoomList);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param znzfCheckApartRoomList
	 * @return
	 */
	@AutoLog(value = "核定单位房源列表-编辑")
	@ApiOperation(value="核定单位房源列表-编辑", notes="核定单位房源列表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZnzfCheckApartRoomList znzfCheckApartRoomList) {
		znzfCheckApartRoomListService.updateById(znzfCheckApartRoomList);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "核定单位房源列表-通过id删除")
	@ApiOperation(value="核定单位房源列表-通过id删除", notes="核定单位房源列表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		znzfCheckApartRoomListService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "核定单位房源列表-批量删除")
	@ApiOperation(value="核定单位房源列表-批量删除", notes="核定单位房源列表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.znzfCheckApartRoomListService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "核定单位房源列表-通过id查询")
	@ApiOperation(value="核定单位房源列表-通过id查询", notes="核定单位房源列表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZnzfCheckApartRoomList> queryById(@RequestParam(name="id",required=true) String id) {
		ZnzfCheckApartRoomList znzfCheckApartRoomList = znzfCheckApartRoomListService.getById(id);
		if(znzfCheckApartRoomList==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(znzfCheckApartRoomList);
	}

	 @AutoLog(value = "核定单位房源列表-通过核定单位id查询房源列表")
	 @ApiOperation(value="核定单位房源列表-通过核定单位id查询房源列表", notes="核定单位房源列表-通过核定单位id查询房源列表")
	 @GetMapping(value = "/searchZnzfRoomByCheckApartId")
	 public Result<Page<ZnzfRoom>> searchZnzfRoomByCheckApartId(HttpServletRequest request, @RequestParam(name="checkDepartId") String checkDepartId,
																	   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String username = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(username);
		 if(username == null || sysUser == null)
			 return Result.error("账号异常");
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.getById(checkDepartId);
		 if(znzfCheckApart == null)
			 return Result.error("房源单位分配异常");
		 List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListService.searchsByZnzfCheckApartId(znzfCheckApart.getId());
		 List<ZnzfRoom> znzfRoomList = new ArrayList<>();
		 if (znzfCheckApartRoomListList.isEmpty()) {
			 Page<ZnzfRoom> page = new Page<>(pageNo, pageSize);
			 Result<Page<ZnzfRoom>> result = new Result<Page<ZnzfRoom>>();
			 page.setSize(pageSize);
			 page.setCurrent(pageNo);
			 page.setTotal(0);
			 page.setPages(1);
			 page.setRecords(znzfRoomList);
			 result.setResult(page);
			 result.setSuccess(true);
			 return result;
		 }
		 for(ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList){
			 ZnzfRoom znzfRoom = znzfRoomService.getById(znzfCheckApartRoomList.getRoomId());
			 znzfRoomList.add(znzfRoom);
		 }
		 Page<ZnzfRoom> page = new Page<>(pageNo, pageSize);
		 int count = znzfRoomList.size();
		 List<ZnzfRoom> pageList = new ArrayList<>();
//计算当前页第一条数据的下标
		 int currId = pageNo > 1 ? (pageSize - 1) * pageSize : 0;
		 for (int i = 0; i < pageSize && i < count - currId; i++) {
			 pageList.add(znzfRoomList.get(currId + i));
		 }
		 page.setSize(pageSize);
		 page.setCurrent(pageNo);
		 page.setTotal(count);
//计算分页总页数
		 page.setPages(count % 10 == 0 ? count / 10 : count / 10 + 1);
		 page.setRecords(pageList);

		 Result<Page<ZnzfRoom>> result = new Result<Page<ZnzfRoom>>();
		 result.setResult(page);
		 result.setSuccess(true);
		 return result;
	 }

	 /**
	  * 通过核定单位id查询核定单位房源列表
	  * @param request
	  * @param checkDepartId
	  * @param pageNo
	  * @param pageSize
	  * @param unit
	  * @param houseNo
	  * @return
	  */
	 @AutoLog(value = "核定单位房源列表-通过核定单位id查询")
	 @ApiOperation(value="核定单位房源列表-通过核定单位id查询", notes="核定单位房源列表-通过核定单位id查询")
	 @GetMapping(value = "/queryByCheckApartId")
	 public Result<Page<ZnzfCheckApartRoomListVo>> queryByCheckApartId(HttpServletRequest request, @RequestParam(name="checkDepartId") String checkDepartId,
																					  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																					  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																					  @RequestParam(name="unit", required = false) String unit,
																	   				@RequestParam(name="houseNo", required = false) String houseNo) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String username = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(username);
		 if(username == null || sysUser == null)
			 return Result.error("账号异常");
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.getById(checkDepartId);
		 if(znzfCheckApart == null)
			 return Result.error("房源单位分配异常");
		 List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListService.searchsByZnzfCheckApartId(znzfCheckApart.getId());
		 List<ZnzfCheckApartRoomListVo> znzfCheckApartRoomListVoList = new ArrayList<>();
		 if (znzfCheckApartRoomListList.isEmpty()) {
			 Page<ZnzfCheckApartRoomListVo> page = new Page<>(pageNo, pageSize);
			 Result<Page<ZnzfCheckApartRoomListVo>> result = new Result<Page<ZnzfCheckApartRoomListVo>>();
			 page.setSize(pageSize);
			 page.setCurrent(pageNo);
			 page.setTotal(0);
			 page.setPages(1);
			 page.setRecords(znzfCheckApartRoomListVoList);
			 result.setResult(page);
			 result.setSuccess(true);
			 return result;
		 }
		 for(ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList){
			 ZnzfCheckApartRoomListVo znzfCheckApartRoomListVo = new ZnzfCheckApartRoomListVo();
			 znzfCheckApartRoomListVo.setCheckApart(znzfCheckApartRoomList.getCheckApart());
			 ZnzfRoom znzfRoom = znzfRoomService.getById(znzfCheckApartRoomList.getRoomId());
			 if(unit != null && !znzfRoom.getUnit().contains(unit)){
			 	continue;
			 }
			 if(houseNo != null && !znzfRoom.getHouseNo().contains(houseNo)){
				 continue;
			 }
			 znzfCheckApartRoomListVo.setZnzfRoom(znzfRoom);
			 if(znzfCheckApartRoomList.getUid() != null){
				 znzfCheckApartRoomListVo.setUid(znzfCheckApartRoomList.getUid());
				 SysUser tmp = sysUserService.getById(znzfCheckApartRoomList.getUid());
				 znzfCheckApartRoomListVo.setName(tmp.getRealname());
				 znzfCheckApartRoomListVo.setTel(tmp.getPhone());

			 }
			 znzfCheckApartRoomListVoList.add(znzfCheckApartRoomListVo);
		 }

		 Page<ZnzfCheckApartRoomListVo> page = new Page<>(pageNo, pageSize);
		 int count = znzfCheckApartRoomListVoList.size();
		 List<ZnzfCheckApartRoomListVo> pageList = new ArrayList<>();
//计算当前页第一条数据的下标
		 int currId = pageNo > 1 ? (pageSize - 1) * pageSize : 0;
		 for (int i = 0; i < pageSize && i < count - currId; i++) {
			 pageList.add(znzfCheckApartRoomListVoList.get(currId + i));
		 }
		 page.setSize(pageSize);
		 page.setCurrent(pageNo);
		 page.setTotal(count);
//计算分页总页数
		 page.setPages(count % 10 == 0 ? count / 10 : count / 10 + 1);
		 page.setRecords(pageList);

		 Result<Page<ZnzfCheckApartRoomListVo>> result = new Result<Page<ZnzfCheckApartRoomListVo>>();
		 result.setResult(page);
		 result.setSuccess(true);
		 return result;
	 }

	 /**
	  *
	  * @param request
	  * @param radId
	  * @param pageNo
	  * @param pageSize
	  * @param unit
	  * @param houseNo
	  * @return
	  */
	 @AutoLog(value = "核定单位房源列表-通过单位房源分配查询")
	 @ApiOperation(value="核定单位房源列表-通过单位房源分配查询", notes="核定单位房源列表-通过单位房源分配查询")
	 @GetMapping(value = "/queryByRadId")
	 public Result<Page<JSONObject>> queryByRadId(HttpServletRequest request, @RequestParam(name="radId") String radId,
																	   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																	   @RequestParam(name="unit", required = false) String unit,
																	   @RequestParam(name="houseNo", required = false) String houseNo) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String username = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(username);
		 if(username == null || sysUser == null)
			 return Result.error("账号异常");
		 SysDepart sysDepart = sysDepartService.searchByOrgCode(sysUser.getOrgCode());
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.searchByZnzfRoomApartDistributeIdAndApartId(radId, sysDepart.getId());
		 if(znzfCheckApart == null)
			 return Result.error("房源单位分配异常");
		 ZnzfRoomApartDistribute znzfRoomApartDistribute = znzfRoomApartDistributeService.getById(radId);
		 if(znzfRoomApartDistribute.getStatus() != 2)
		 	return Result.error("当前还不能设置中签");
		 List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListService.searchsByZnzfCheckApartId(znzfCheckApart.getId());
		 List<JSONObject> jsonObjectList = new ArrayList<>();
		 if (znzfCheckApartRoomListList.isEmpty()) {
			 Page<JSONObject> page = new Page<>(pageNo, pageSize);
			 Result<Page<JSONObject>> result = new Result<Page<JSONObject>>();
			 page.setSize(pageSize);
			 page.setCurrent(pageNo);
			 page.setTotal(0);
			 page.setPages(1);
			 page.setRecords(jsonObjectList);
			 result.setResult(page);
			 result.setSuccess(true);
			 return result;
		 }
		 for(ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList){
		 	JSONObject jsonObject = new JSONObject();
		 	jsonObject.put("id",znzfCheckApartRoomList.getId());
			 jsonObject.put("roomId",znzfCheckApartRoomList.getRoomId());
			 jsonObject.put("uid",znzfCheckApartRoomList.getUid());
			 jsonObject.put("checkApart",znzfCheckApartRoomList.getCheckApart());
			 if(oConvertUtils.isNotEmpty(znzfCheckApartRoomList.getUid())){
				 SysUser user = sysUserService.getById(znzfCheckApartRoomList.getUid());
				 jsonObject.put("realname",user.getRealname());
				 jsonObject.put("phone",user.getPhone());
				 jsonObject.put("email",user.getEmail());
				 jsonObject.put("sex",user.getSex());
				 jsonObject.put("telephone",user.getTelephone());
			 }
			 ZnzfRoom znzfRoom = znzfRoomService.getById(znzfCheckApartRoomList.getRoomId());
			 jsonObject.put("houseNo",znzfRoom.getHouseNo());
			 if(houseNo != null && !znzfRoom.getHouseNo().contains(houseNo))
			 	continue;
			 jsonObject.put("unit",znzfRoom.getUnit());
			 if(unit != null && !znzfRoom.getUnit().contains(unit))
				 continue;
			 jsonObject.put("groupName",znzfRoom.getGroupName());
			 jsonObjectList.add(jsonObject);
		 }

		 Page<JSONObject> page = new Page<>(pageNo, pageSize);
		 int count = jsonObjectList.size();
		 List<JSONObject> pageList = new ArrayList<>();
//计算当前页第一条数据的下标
		 int currId = pageNo > 1 ? (pageSize - 1) * pageSize : 0;
		 for (int i = 0; i < pageSize && i < count - currId; i++) {
			 pageList.add(jsonObjectList.get(currId + i));
		 }
		 page.setSize(pageSize);
		 page.setCurrent(pageNo);
		 page.setTotal(count);
//计算分页总页数
		 page.setPages(count % 10 == 0 ? count / 10 : count / 10 + 1);
		 page.setRecords(pageList);

		 Result<Page<JSONObject>> result = new Result<Page<JSONObject>>();
		 result.setResult(page);
		 result.setSuccess(true);
		 return result;
	 }


	 /**
    * 导出excel
    *
    * @param request
    * @param znzfCheckApartRoomList
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZnzfCheckApartRoomList znzfCheckApartRoomList) {
        return super.exportXls(request, znzfCheckApartRoomList, ZnzfCheckApartRoomList.class, "核定单位房源列表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZnzfCheckApartRoomList.class);
    }

	 /**
	  * 更新房源列表
	  * @param caId
	  * @param roomIds
	  * @return
	  */
	 @AutoLog(value = "核定单位房源列表-更新房源")
	 @ApiOperation(value="核定单位房源列表-更新房源", notes="核定单位房源列表-更新房源")
	 @PostMapping(value = "/update")
	 public Result<String> update(@RequestParam("caId") String caId, @RequestParam("roomIds") String[] roomIds) {
    	List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListService.searchsByZnzfCheckApartId(caId);
    	if(znzfCheckApartRoomListList != null){
    		for(ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList){
    			znzfCheckApartRoomListService.removeById(znzfCheckApartRoomList.getId());
			}
		}
    	for(String roomId : roomIds){
    		ZnzfCheckApartRoomList znzfCheckApartRoomList = new ZnzfCheckApartRoomList();
    		znzfCheckApartRoomList.setCheckApart(caId);
    		znzfCheckApartRoomList.setRoomId(roomId);
    		znzfCheckApartRoomListService.save(znzfCheckApartRoomList);
		}

		 return Result.OK("更新成功！");
	 }

	 /**
	  * 通过单位房源分配查询房源列表信息
	  * @param request
	  * @param radId
	  * @return
	  */
	 @AutoLog(value = "核定单位房源列表-通过单位房源分配id查询房源列表信息")
	 @ApiOperation(value="核定单位房源列表-通过单位房源分配id查询房源列表信息", notes="房源单位分配-通过单位房源分配id查询房源列表信息")
	 @GetMapping(value = "/znzfApplyToZnzfRoomApartDistribute")
	 public Result<Page<ZnzfRoom>> znzfApplyToZnzfRoomApartDistribute(HttpServletRequest request, @RequestParam(name="radId") String radId,
															  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																@RequestParam(name="name", required = false) String name) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String username = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(username);
		 if(username == null || sysUser == null)
			 return Result.error("账号异常");
		 ZnzfRoomApartDistribute znzfRoomApartDistribute = znzfRoomApartDistributeService.getById(radId);
		 if(znzfRoomApartDistribute == null)
			 return Result.error("房源单位分配异常");
		 ZnzfApply znzfApply = znzfApplyService.getById(znzfRoomApartDistribute.getApplyInfo());
		 ZnzfProject znzfProject = znzfProjectService.getById(znzfApply.getProject());
		 List<ZnzfRoom> znzfRoomList = znzfRoomService.selectByMainId(znzfProject.getId());
//		 List<JSONObject> jsonObjectList = new ArrayList<>();
		 if (znzfRoomList.isEmpty()) {
			 Page<ZnzfRoom> page = new Page<>(pageNo, pageSize);
			 Result<Page<ZnzfRoom>> result = new Result<Page<ZnzfRoom>>();
			 page.setSize(pageSize);
			 page.setCurrent(pageNo);
			 page.setTotal(0);
			 page.setPages(1);
			 page.setRecords(znzfRoomList);
			 result.setResult(page);
			 result.setSuccess(true);
			 return result;
		 }

		 for(ZnzfRoom znzfRoom : znzfRoomList){

//			 System.out.println(znzfRoom.getBuildTime() + "\n");
//			 JSONObject object = (JSONObject)JSONObject.toJSON(znzfRoom);
//			 System.out.println(object.get("buildTime")+ "\n");
			 znzfRoom.setState_dictText("正常");
//			 switch (znzfRoom.getState()){
//				 case 0:{
//					 object.put("state_dictText", "正常");
//				 }break;
//				 case 1:{
//					 object.put("state_dictText", "维修");
//				 }break;
//				 case 2:{
//					 object.put("state_dictText", "冻结");
//				 }break;
//				 case 4:{
//					 object.put("state_dictText", "待使用");
//				 }break;
//			 }
		 	//之后高级查询接口
//			 jsonObjectList.add(object);
		 }

		 Page<ZnzfRoom> page = new Page<>(pageNo, pageSize);
		 int count = znzfRoomList.size();
		 List<ZnzfRoom> pageList = new ArrayList<>();
//计算当前页第一条数据的下标
		 int currId = pageNo > 1 ? (pageSize - 1) * pageSize : 0;
		 for (int i = 0; i < pageSize && i < count - currId; i++) {
			 pageList.add(znzfRoomList.get(currId + i));
		 }
		 page.setSize(pageSize);
		 page.setCurrent(pageNo);
		 page.setTotal(count);
//计算分页总页数
		 page.setPages(count % 10 == 0 ? count / 10 : count / 10 + 1);
		 page.setRecords(pageList);

		 Result<Page<ZnzfRoom>> result = new Result<Page<ZnzfRoom>>();
		 result.setResult(page);
		 result.setSuccess(true);
		 return result;
	 }

	 @AutoLog(value = "核定单位房源列表-保存和订单为一一对应的分配结果")
	 @ApiOperation(value="核定单位房源列表-保存和订单为一一对应的分配结果", notes="房源单位分配-保存和订单为一一对应的分配结果")
	 @PostMapping(value = "/saveRoomToEmployee")
	 public Result<String> saveRoomToEmployee(HttpServletRequest request, @RequestBody ZnzfCheckApartRoomListParam znzfCheckApartRoomListParam) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String name = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(name);
		 if(name == null || sysUser == null)
			 return Result.error("账号异常");
		 List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListParam.getCheckApartRoomList();
		 for(ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList){
			znzfCheckApartRoomListService.updateById(znzfCheckApartRoomList);
		 }
		 return Result.ok("更新分配结果成功");
	 }

	 @AutoLog(value = "核定单位房源列表-提交分配结果")
	 @ApiOperation(value="核定单位房源列表-提交分配结果", notes="房源单位分配-提交分配结果")
	 @PostMapping(value = "/submitRoomToEmployee")
	 public Result<String> submitRoomToEmployee(HttpServletRequest request, @RequestBody ZnzfCheckApartRoomListParam znzfCheckApartRoomListParam) {
	 	String radId = znzfCheckApartRoomListParam.getRadId();
		 List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListParam.getCheckApartRoomList();
		 if(znzfCheckApartRoomListList == null)
			 return Result.error("无数据提交");
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String name = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(name);
		 if(name == null || sysUser == null)
			 return Result.error("账号异常");
		 ZnzfRoomApartDistribute znzfRoomApartDistribute = znzfRoomApartDistributeService.getById(radId);
		 if(znzfRoomApartDistribute == null)
			 return Result.error("房源单位分配异常");
		 SysDepart sysDepart = sysDepartService.searchByOrgCode(sysUser.getOrgCode());
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.searchByZnzfRoomApartDistributeIdAndApartId(radId, sysDepart.getId());
		 if(znzfCheckApart == null)
			 return Result.error("核定部门异常");
		 for(ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList){
			 znzfCheckApartRoomListService.updateById(znzfCheckApartRoomList);
		 }
		 znzfCheckApart.setState(1 + "");
		 znzfCheckApartService.updateById(znzfCheckApart);
		 return Result.ok("提交分配结果成功");
	 }

	 @AutoLog(value = "核定单位房源列表-分配房源给核定单位")
	 @ApiOperation(value="核定单位房源列表-分配房源给核定单位", notes="房源单位分配-分配房源给核定单位")
	 @PostMapping(value = "/distributeRoom")
	 public Result<Page<ZnzfCheckApartRoomListVo>> distributeRoom(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
	 	 String caId = jsonObject.getString("caId");
	 	 String[] roomIds = jsonObject.getJSONArray("roomIds").toArray(new String[0]);
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String name = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(name);
		 if(name == null || sysUser == null)
			 return Result.error("账号异常");
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.getById(caId);
		 if(znzfCheckApart == null)
			 return Result.error("核定单位异常");
		 if(znzfCheckApart.getCheckedRoomNum() < roomIds.length)
		 	return Result.error("分配房源最大数为" + znzfCheckApart.getCheckedRoomNum() + "个");
		 for(String roomid : roomIds){
			 List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListService.searchsByRoomId(roomid);
			 if(!znzfCheckApartRoomListList.isEmpty()){
				 for (ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList) {
					 if (Objects.equals(znzfCheckApartRoomList.getCheckApart(), caId))
						 continue;
					 ZnzfRoom znzfRoom = znzfRoomService.getById(znzfCheckApartRoomList.getRoomId());
					 return Result.error(znzfRoom.getHouseNo() + "幢" + znzfRoom.getUnit() + "单元" + znzfRoom.getRoomNo() + "号房已被选");
				 }
			 }
	 	}
		 List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListService.searchsByZnzfCheckApartId(caId);

		 if(!znzfCheckApartRoomListList.isEmpty()){
			 String ids = "";
			 for(ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList){
			 	ZnzfRoom znzfRoom = znzfRoomService.getById(znzfCheckApartRoomList.getRoomId());
			 	znzfRoom.setState(0);
			 	znzfRoomService.updateById(znzfRoom);
			 	ids = ids + znzfCheckApartRoomList.getId() + ",";
			 }
			 ids.substring(0,ids.length()-1);
			 znzfCheckApartRoomListService.removeByIds(Arrays.asList(ids.split(",")));
		 }

		 for(String roomid : roomIds){
		 	ZnzfCheckApartRoomList znzfCheckApartRoomList = new ZnzfCheckApartRoomList();
		 	znzfCheckApartRoomList.setRoomId(roomid);
		 	znzfCheckApartRoomList.setCheckApart(znzfCheckApart.getId());
		 	znzfCheckApartRoomListService.save(znzfCheckApartRoomList);
		 	ZnzfRoom znzfRoom = znzfRoomService.getById(roomid);
		 	znzfRoom.setState(3);
		 	znzfRoomService.updateById(znzfRoom);
		 }
		 return Result.ok("分配房源成功");
	 }

	 @AutoLog(value = "核定单位房源列表-通过房源id和核定单位id删除房源列表")
	 @ApiOperation(value="核定单位房源列表-通过房源id和核定单位id删除房源列表", notes="房源单位分配-通过房源id和核定单位id删除房源列表")
	 @DeleteMapping(value = "/deleteByRoomIdAndCheckApartId")
	 public Result<Page<ZnzfCheckApartRoomListVo>> deleteByRoomIdAndCheckApartId(HttpServletRequest request,@RequestParam(name="roomId",required=true) String roomId,
																				 @RequestParam(name="checkApartId",required=true) String checkApartId) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String name = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(name);
		 if(name == null || sysUser == null)
			 return Result.error("账号异常");
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.getById(checkApartId);
		 if(znzfCheckApart == null)
			 return Result.error("核定单位异常");
		 ZnzfCheckApartRoomList znzfCheckApartRoomList = znzfCheckApartRoomListService.searchByRoomIdAndCheckApartId(roomId, checkApartId);
		 znzfCheckApartRoomListService.removeById(znzfCheckApartRoomList.getId());
		 ZnzfRoom znzfRoom = znzfRoomService.getById(roomId);
		 znzfRoom.setState(0);
		 znzfRoomService.updateById(znzfRoom);
		 return Result.ok("删除房源列表成功");
	 }

	 @AutoLog(value = "核定单位房源列表-通过房源id和核定单位id批量删除房源列表")
	 @ApiOperation(value="核定单位房源列表-通过房源id和核定单位id批量删除房源列表", notes="房源单位分配-通过房源id和核定单位id批量删除房源列表")
	 @DeleteMapping(value = "/deletesByRoomIdAndCheckApartId")
	 public Result<Page<ZnzfCheckApartRoomListVo>> deletesByRoomIdAndCheckApartId(HttpServletRequest request,@RequestParam(name="roomIds",required=true) String roomIds,
																				  @RequestParam(name="checkApartId",required=true) String checkApartId) {
	 	String[] roomIdArray = roomIds.split(",");
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String name = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(name);
		 if(name == null || sysUser == null)
			 return Result.error("账号异常");
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.getById(checkApartId);
		 if(znzfCheckApart == null)
			 return Result.error("核定单位异常");
		 if(roomIdArray.length == 0)
		 	return Result.error("请选择房源");
		 for(String roomId : roomIdArray){
			 ZnzfCheckApartRoomList znzfCheckApartRoomList = znzfCheckApartRoomListService.searchByRoomIdAndCheckApartId(roomId, checkApartId);
			 znzfCheckApartRoomListService.removeById(znzfCheckApartRoomList.getId());
			 ZnzfRoom znzfRoom = znzfRoomService.getById(roomId);
			 znzfRoom.setState(0);
			 znzfRoomService.updateById(znzfRoom);
		 }
		 return Result.ok("删除房源列表成功");
	 }

	 @AutoLog(value = "核定单位房源列表-一键随机分配中签")
	 @ApiOperation(value="核定单位房源列表-一键随机分配中签", notes="核定单位房源列表-一键随机分配中签")
	 @PostMapping(value = "/distributeByRandom")
	 public Result<Page<ZnzfCheckApartRoomListVo>> distributeByRandom(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
	 	String radId = jsonObject.getString("radId");
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String username = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(username);
		 if(username == null || sysUser == null)
			 return Result.error("账号异常");
		 SysDepart sysDepart = sysDepartService.searchByOrgCode(sysUser.getOrgCode());
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.searchByZnzfRoomApartDistributeIdAndApartId(radId, sysDepart.getId());
		 if(znzfCheckApart == null)
			 return Result.error("房源单位分配异常");
		 ZnzfRoomApartDistribute znzfRoomApartDistribute = znzfRoomApartDistributeService.getById(radId);
		 if(znzfRoomApartDistribute.getStatus() != 2)
			 return Result.error("当前还不能设置中签");
		 List<ZnzfCheckEmployee> znzfCheckEmployeeList = znzfCheckEmployeeService.findCheckedEmployListByCheckApartId(znzfCheckApart.getId());
		 if(znzfCheckEmployeeList.isEmpty())
			 return Result.error("未有人入围");
		 List<ZnzfCheckApartRoomList> znzfCheckApartRoomListList = znzfCheckApartRoomListService.searchsByZnzfCheckApartId(znzfCheckApart.getId());
		 if(znzfCheckApartRoomListList.isEmpty())
			 return Result.error("未分配房源");
		 int employeeNum = znzfCheckEmployeeList.size();
		 int roomNum = znzfCheckApartRoomListList.size();
		 if(roomNum < employeeNum){
			 znzfCheckEmployeeList = znzfCheckEmployeeService.findCheckedEmployListByCheckApartIdAndNum(znzfCheckApart.getId(), roomNum);
		 }
		 for(ZnzfCheckApartRoomList znzfCheckApartRoomList : znzfCheckApartRoomListList){
		 	if(znzfCheckEmployeeList.isEmpty())
		 		break;
			 Random random = new Random();
			 int size = znzfCheckEmployeeList.size();
			 int intRandom = random.nextInt(size);
			 ZnzfCheckEmployee znzfCheckEmployee = znzfCheckEmployeeList.get((intRandom + size - 1) % size);
			 znzfCheckApartRoomList.setUid(znzfCheckEmployee.getUserId());
			 znzfCheckApartRoomListService.updateById(znzfCheckApartRoomList);
			 znzfCheckEmployeeList.remove(znzfCheckEmployeeList.get((intRandom + size - 1) % size));
		 }
		 return Result.ok("一键分配中签成功");
	 }
}
